package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.entity.YddUser;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

//@CrossOrigin
@RestController
@RequestMapping("/home/index")
@Slf4j
public class IndexController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public R<Object> login(@RequestBody YddUser yddUser){
        log.info("SysUser-{}",yddUser);
        String mobile = yddUser.getMobile();
        LambdaQueryWrapper<YddUser> ylqw = new LambdaQueryWrapper<>();
        ylqw.eq(YddUser::getMobile,yddUser.getMobile());
        YddUser userServiceOne = userService.getOne(ylqw);
        if(userServiceOne==null){
            return R.error("用户不存在",1000);
        }
        if(!DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes()).equals(userServiceOne.getPassword())){
            return R.error("密码错误",1000);
        }
        HashMap<String, String> map = new HashMap<>();
        map.put( "token",userServiceOne.getId().toString());
        return R.success(map);
    }
}
